package com.cheyun.netsalev3.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.utils.FunctionUtils;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;

/**
 * 首页头部嵌套滑动
 */
public class MyTopBehavior extends ViewOffsetBehavior {

    //使用当前滑动器View的高度
    private int height;

    public MyTopBehavior() {
    }

    public MyTopBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 接收到嵌套滚动请求，如果滚动方向是自己需要的则同意嵌套滚动，这时一般主动放弃拦截MOVE事件
     */
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        height = child.getMeasuredHeight() + FunctionUtils.dip2px(coordinatorLayout.getContext(), 15f);
        height = height / 2;
        return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    /**
     * 接收到预滚动请求， dy<0 向下；dy>0 向上
     */
    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        //dy<0 向下；dy>0 向上
        if (dy < 0)
            return;
        float halfOfDis = dy / 2.0f;
        if (!canScroll(child, halfOfDis)) {
            child.setTranslationY(dy > 0 ? -height : 0);
        } else {
            child.setTranslationY(child.getTranslationY() - halfOfDis);
//            child.setAlpha(1-Math.abs(child.getTranslationY()/height*0.9f));
            consumed[1] = dy;
        }


    }

    /**
     * 是否可以滑动 判断在滑动的范围
     */
    private boolean canScroll(View child, float dy) {
        int pendingTranslationY = (int) (child.getTranslationY() - dy);
        if (-pendingTranslationY >= 0 && -pendingTranslationY <= height) {
            return true;
        }
        return false;
    }


    private int getHeaderOffsetRange() {
        return MyApplication.getInstance().getResources().getDimensionPixelOffset(R.dimen.home_top);
    }

    /**
     * 接收到滚动请求，此时可以主动滑动来消费掉发起方提供的未消费距离
     */
    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
        if (dyConsumed == 0 && dyUnconsumed != 0) {
            float halfOfDis = dyUnconsumed / 2.0f;
            if (!canScroll(child, halfOfDis)) {
                child.setTranslationY(dyUnconsumed > 0 ? -height : 0);
            } else {
                child.setTranslationY(child.getTranslationY() - halfOfDis);
//                child.setAlpha(1-Math.abs(child.getTranslationY()/height*0.9f));
            }
        }
    }

    /**
     * 接收到甩动请求，一般如果发起方声明未消费甩动则自己可以执行甩动操作
     */
    @Override
    public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, float velocityX, float velocityY, boolean consumed) {
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }
}
